package F.树;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

public class _剑指Offer32II_从上到下打印二叉树II {
    public List<List<Integer>> levelOrder(TreeNode root) {

        List<List<Integer>> result = new LinkedList<>();

        if (root == null){
            return result;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new LinkedList<>();
            while (size>0){
                TreeNode node = queue.poll();
                list.add(node.val);

                if (node.left!= null){
                    queue.add(node.left);
                }
                if (node.right!= null){
                    queue.add(node.right);
                }
                size--;
            }

            result.add(list);


        }

        return result;


    }

}
